package org.apache.solr.search.similarities;

import org.apache.lucene.search.similarities.BM25Similarity;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.schema.SimilarityFactory;

/**
 * Factory for {@link BM25Similarity}
 * <p>
 * Parameters:
 * <ul>
 *   <li>k1 (float): Controls non-linear term frequency normalization (saturation).
 *                   The default is <code>1.2</code>
 *   <li>b (float): Controls to what degree document length normalizes tf values.
 *                  The default is <code>0.75</code>
 * </ul>
 * <p>
 * Optional settings:
 * <ul>
 *   <li>discountOverlaps (bool): Sets
 *       {@link BM25Similarity#setDiscountOverlaps(boolean)}</li>
 * </ul>
 * @lucene.experimental
 */
public class BM25SimilarityFactory extends SimilarityFactory {

    private boolean discountOverlaps;
    private float k1;
    private float b;

    @Override
    public void init(SolrParams params) {
        super.init(params);

        discountOverlaps = params.getBool("discountOverlaps", true);
        k1 = params.getFloat("k1", 1.2f);
        b = params.getFloat("b", 0.75f);
    }

    @Override
    public Similarity getSimilarity() {

        BM25Similarity sim = new BM25Similarity(k1, b);
        sim.setDiscountOverlaps(discountOverlaps);
        return sim;
    }
}
